package com.monise.dao.DAO;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.monise.dao.BaseDao.GenericDao;
import com.monise.model.Cliente;
import com.monise.model.ClienteAnimal;

public class ClienteDAO extends GenericDao<Cliente>{

	public Cliente getClientes(ClienteAnimal clienteAnimal) {
		EntityManager manager = getEntityManager();

		manager.getTransaction().begin();
		Query query = manager
				.createQuery("SELECT a from Cliente a where a.pkCliente=:cliente");
		query.setParameter("cliente", clienteAnimal.getCliente().getPkCliente());

		Cliente clientes = (Cliente) query.getSingleResult();

		manager.getTransaction().commit();
		manager.close();

		return clientes;
	}

	public Long getId() {
		EntityManager manager = getEntityManager();

		manager.getTransaction().begin();
		Query query = manager.createQuery("SELECT MAX(pkCliente) from Cliente");

		Long cliente = (Long) query.getSingleResult();

		manager.getTransaction().commit();
		manager.close();

		if (cliente == null) {
			return Long.parseLong("1");
		} else {
			return cliente + 1;
		}
	}
	
	/*public Cliente getCliente(Long codigo){
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("pet");
		EntityManager manager = factory.createEntityManager();
		
		manager.getTransaction().begin();
        Query query = manager.createQuery("SELECT a from Cliente a where a.pkCliente=:codigo");
		query.setParameter("codigo",codigo);

		Cliente cliente = (Cliente) query.getSingleResult();

		manager.getTransaction().commit();
		manager.close();

		return cliente;
	}
*/
}
